﻿using System;
using System.Collections.Generic;
using TopBuilds.Restaurant.Data;

namespace TopBuilds.Restaurant.ViewModel
{
    public sealed class PrivilegeCollection : System.Collections.ObjectModel.ObservableCollection<Privilege>
    {
        private string _condition;

        public string Condition
        {
            get { return _condition; }

            set
            {
                if (_condition != null && _condition == value)
                {
                    return;
                }

                _condition = value;

                if (Count > 0)
                {
                    base.Clear();
                }

                Privilege[] list = PrivilegeObject.GetPrivileges();
                if (list != null)
                {
                    foreach (Privilege item in list)
                    {
                        Add(item);
                    }
                }
            }
        }


        public PrivilegeCollection()
        {
            Condition = null;
        }


        public Privilege GetPrivilege(int privilegeId)
        {
            foreach (Privilege privilege in this)
            {
                if (privilege.Id == privilegeId)
                {
                    return privilege;
                }
            }

            return null;
        }
    }
}
